package com.atguigu.service.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.dao.impl.OrderDaoImpl;
import com.atguigu.dao.impl.OrderItemDaoImpl;
import com.atguigu.pojo.*;
import com.atguigu.service.OrderService;

import java.awt.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author YAO
 * @create 2021-05-13 10:12
 */
public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();
    private OrderItemDao orderItemDao = new OrderItemDaoImpl();
    private BookDao bookDao = new BookDaoImpl();

    @Override
    public String createOrder(Cart cart, Integer userId) {
        System.out.println("OrderServiceImpl程序在[" + Thread.currentThread().getName() + "]中");

        //订单号（唯一）
        String orderId = System.currentTimeMillis() + userId + "";
        //创建一个订单对象
        Order order = new Order(orderId, new Date(), cart.getTotalPrice(), 0, userId);
        //保存订单
        orderDao.saveOrder(order);

        //遍历购物车中的items，把每一项转化为订单项并保存
        for (Map.Entry<Integer, CartItem> entry : cart.getItems().entrySet()) {
            //获取item
            CartItem item = entry.getValue();
            //转化为orderItem
            OrderItem orderItem = new OrderItem(null, item.getName(), item.getCount(), item.getPrice(),
                    item.getTotalPrice(), orderId);
            //保存orderItem
            orderItemDao.saveOrderItem(orderItem);

            //更新库存与销量
            Integer bookId = item.getId();
            Book book = bookDao.queryBookById(bookId);
            book.setSales(book.getSales() + item.getCount());
            book.setStock(book.getStock() - item.getCount());
            bookDao.updateBook(book);
        }

        //清空购物车
        cart.clear();

        return orderId;
    }

    @Override
    public List<Order> showAllOrders() {
        return orderDao.queryOrders();
    }

    @Override
    public boolean sendOrder(String orderId) {
        int i = orderDao.changeOrderStatus(orderId, 1);
        if(i != -1){
            return true;
        }
        return false;
    }

    @Override
    public List<OrderItem> showOrderDetail(String orderId) {
        List<OrderItem> orderItems = orderItemDao.queryOrderItemsByOrderId(orderId);
        return orderItems;
    }

    @Override
    public List<Order> showMyOrders(Integer userId) {
        return orderDao.queryOrdersByUserId(userId);
    }

    @Override
    public boolean receiveOrder(String orderId) {
        int i = orderDao.changeOrderStatus(orderId, 2);
        return i != -1;
    }

    @Override
    public Page<Order> page(Integer pageNo, Integer pageSize) {
        Page<Order> page = new Page<>();

        //设置网页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount = orderDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        int pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize > 0){
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);


        //设置当前页码
        page.setPageNo(pageNo);

        int begin = (page.getPageNo() - 1) * pageSize;
        //求当前页数据
        List<Order> items = orderDao.queryForPageItems(begin, pageSize);
        //设置当前页数据
        page.setItems(items);

        return page;
    }

    @Override
    public void deleteOrdersByUserId(Integer userId) {
        orderDao.deleteOrdersByUserId(userId);
    }

    @Override
    public void deleteOrderItemsByOrderId(String orderId) {
        orderDao.deleteOrderItemsByOrderId(orderId);
    }

    @Override
    public void deleteOrderByOrderId(String orderId) {
        orderDao.deleteOrderByOrderId(orderId);
    }
}
